package com.wjjun.test.ui

import android.view.View
import androidx.databinding.ViewDataBinding
import com.wjjun.base.ui.activity.BaseListActivity
import com.wjjun.base.ui.helper.RecycleViewHelper
import com.wjjun.base.ui.viewmodel.BaseItemViewModel
import com.wjjun.base.ui.viewmodel.BaseViewModel
import com.wjjun.app.R
import com.wjjun.test.ui.itemviewmodel.TestItemViewModel
import com.wjjun.test.ui.viewmodel.TestMenuViewModel
import com.wjjun.test.ui.viewmodel.TestNestListViewModel

/**
 * 测试嵌套滚动
 * author : wangjunjun
 * date   : 2022/6/29
 * desc   :
 */
class TestNestListActivity : BaseListActivity<TestNestListViewModel, ViewDataBinding>() {

    private val recycleViewHelper = RecycleViewHelper()

    var checkedViewModel: TestMenuViewModel? = null

    override fun initContent() {
        setTitle("测试嵌套RecycleView")
        setRefreshEnable(true)
        setLoadMoreEnable(true)
        recycleViewHelper.setLifecycleOwner(this)
        recycleViewHelper.onItemClickListener = this

        viewModel.menus.set(recycleViewHelper.buildViewModel())
    }

    override fun initParams() {
    }

    override suspend fun fetchData() {
        val menuList = ArrayList<BaseItemViewModel<*>>()
        menuList.add(TestMenuViewModel("菜单1"))
        menuList.add(TestMenuViewModel("菜单2"))
        menuList.add(TestMenuViewModel("菜单3"))
        menuList.add(TestMenuViewModel("菜单4"))
        menuList.add(TestMenuViewModel("菜单5"))
        menuList.add(TestMenuViewModel("菜单6"))

        checkedViewModel = menuList[0] as TestMenuViewModel

        recycleViewHelper.setData(menuList)

        refreshData()
    }

    override fun onClick(view: View) {
    }

    override suspend fun fetchData(pageNum: Int, pageSize: Int, useCache: Boolean, param: Any?) {
        val data = ArrayList<BaseItemViewModel<*>>()
        data.add(recycleViewHelper.buildNestViewModel(R.layout.item_nest_list))

        for (i in 0..10) {
            data.add(TestItemViewModel("数据${i} :" + checkedViewModel?.getData()))
        }

        setData(data, pageNum)
        finishLoad()
    }

    override fun onItemClick(view: View, itemViewModel: BaseViewModel) {
        when (itemViewModel) {
            is TestMenuViewModel -> {
                checkedViewModel = itemViewModel
                refreshData()
            }
            is TestItemViewModel -> {
                when (view.id) {
                    R.id.tv_edit -> {
                        itemViewModel.editData {
                            it?.name = "编辑1后${System.currentTimeMillis()}"
                        }
                    }
                    R.id.tv_delete -> {
                        removeItem(itemViewModel)
                    }
                }
            }
        }
    }
}